package com.simpleaccount.Home;

import android.animation.ObjectAnimator;
import android.content.Intent;
import android.media.Image;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.daimajia.androidanimations.library.BaseViewAnimator;
import com.daimajia.androidanimations.library.Techniques;
import com.daimajia.androidanimations.library.YoYo;
import com.liaoinstan.springview.container.BaseHeader;
import com.liaoinstan.springview.container.DefaultFooter;
import com.liaoinstan.springview.container.DefaultHeader;
import com.liaoinstan.springview.container.RotationHeader;
import com.liaoinstan.springview.widget.SpringView;
import com.simpleaccount.Add.AddActivity;
import com.simpleaccount.Common.DateUtils;
import com.simpleaccount.Db.DbUtils;
import com.simpleaccount.Entity.Entry;
import com.simpleaccount.R;
import com.simpleaccount.Search.SearchActivity;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;

//import com.jcodecraeer.xrecyclerview.ProgressStyle;
//import com.jcodecraeer.xrecyclerview.XRecyclerView;
//import com.jcodecraeer.xrecyclerview.ProgressStyle;
//import com.jcodecraeer.xrecyclerview.XRecyclerView;
//import com.marshalchen.ultimaterecyclerview.UltimateRecyclerView;

/**
 * Created by Administrator on 2017/7/12.
 */

public class HomeFragment extends Fragment {
    @Bind(R.id.rv1)
    RecyclerView rv1;
    @Bind(R.id.civ_add)
    CircleImageView civAdd;
    @Bind(R.id.iv_search)
    ImageView ivSearch;
    @Bind(R.id.sv_1)
    SpringView sv1;
    @Bind(R.id.tv_month_income)
    TextView tvMonthIncome;
    @Bind(R.id.tv_money_income)
    TextView tvMoneyIncome;
    @Bind(R.id.tv_month_outcome)
    TextView tvMonthOutcome;
    @Bind(R.id.tv_money_outcome)
    TextView tvMoneyOutcome;

    int cntEntriesShown = 10;
    List<Entry> entries;
    MyRecyclerViewAdapter adapter;
    static final int REFRESH_TIME_MIN = 800;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        entries = DbUtils.getEntry(10);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        final View view = inflater.inflate(R.layout.fragment_home, container, false);
        ButterKnife.bind(this, view);

        initRecyclerView();//瀑布流
        sv1.setType(SpringView.Type.FOLLOW);
        sv1.setHeader(new BaseHeader() {
            List<Integer> lens=new ArrayList<>();
            @Override
            public View getView(LayoutInflater inflater, ViewGroup viewGroup) {
                return inflater.inflate(R.layout.base_header, viewGroup, true);
            }

            @Override
            public void onPreDrag(View rootView) {
                civAdd.setRotation(0);
            }
            @Override
            public void onDropAnim(View rootView, int dy) {
                //civAdd.setRotation(dy);

            }

            @Override
            public void onLimitDes(View rootView, boolean upORdown) {
                Intent intent = new Intent();
                intent.setClass(getActivity(), AddActivity.class);
                startActivityForResult(intent, 0);
            }

            @Override
            public void onStartAnim() {

            }
            @Override
            public void onFinishAnim() {

            }
        });
        sv1.setFooter(new DefaultFooter(getContext()));
        sv1.setListener(new SpringView.OnFreshListener() {
            @Override
            public void onRefresh() {
                sv1.onFinishFreshAndLoad();
            }

            @Override
            public void onLoadmore() {
                cntEntriesShown += 10;
                new Handler().postDelayed(new Runnable() {
                    public void run() {
                        entries = DbUtils.getEntry(cntEntriesShown);
                        adapter.notifyData(entries);
                        sv1.onFinishFreshAndLoad();
                    }
                }, 800);
            }
        });
        return view;
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    private void initRecyclerView() {
        adapter = new MyRecyclerViewAdapter(entries, getContext());
        WrapContentLinearLayoutManager wcllm = new WrapContentLinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
        rv1.setHasFixedSize(true);
        rv1.setLayoutManager(wcllm);
        rv1.setAdapter(adapter);
        //rv1.addItemDecoration(new MyItemDecoration(20));
        rv1.setItemAnimator(new DefaultItemAnimator()); // 默认动画
    }

    @OnClick({R.id.civ_add, R.id.iv_search})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.civ_add:
                Intent intent = new Intent();
                intent.setClass(getActivity(), AddActivity.class);
                startActivityForResult(intent, 0);
                break;
            case R.id.iv_search:
                Intent intent_search = new Intent();
                intent_search.setClass(getActivity(), SearchActivity.class);
                startActivity(intent_search);
                break;
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        cntEntriesShown=10;
        if(null!=adapter){
            adapter.notifyData(DbUtils.getEntry(10));
        }
        double income=0;
        double outcome =0;
        Calendar calendar = Calendar.getInstance();
        List<Entry> entryList=DbUtils.queryByDate(DateUtils.getTimesMonthmorning(calendar),DateUtils.getTimesMonthnight(calendar));
        for(int i=0;i<entryList.size();i++){
            Entry e = entryList.get(i);
            if(0==e.getType()){
                outcome+=e.getMoney();
            }else if(1==e.getType()){
                income+=e.getMoney();
            }
        }
        tvMoneyIncome.setText(String.format(Locale.CHINA,"%.2f",income));
        tvMoneyOutcome.setText(String.format(Locale.CHINA,"%.2f",Math.abs(outcome)));
    }
    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        cntEntriesShown=10;
        if(null!=adapter){
            adapter.notifyData(DbUtils.getEntry(10));
        }
        double income=0;
        double outcome =0;
        Calendar calendar = Calendar.getInstance();
        List<Entry> entryList=DbUtils.queryByDate(DateUtils.getTimesMonthmorning(calendar),DateUtils.getTimesMonthnight(calendar));
        for(int i=0;i<entryList.size();i++){
            Entry e = entryList.get(i);
            if(0==e.getType()){
                outcome+=e.getMoney();
            }else if(1==e.getType()){
                income+=e.getMoney();
            }
        }
        tvMoneyIncome.setText(String.format(Locale.CHINA,"%.2f",income));
        tvMoneyOutcome.setText(String.format(Locale.CHINA,"%.2f",Math.abs(outcome)));
    }
}
